package br.edu.ifrn.utineo.web;

import br.edu.ifrn.utineo.jdbc.Conexao;
import br.edu.ifrn.utineo.modelo.Pacientes;
import br.edu.ifrn.utineo.modelo.Prescricao;
import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.*;
import javax.servlet.RequestDispatcher;


/**
 * Servlet implementation class CadastrarPacienteServlet
 */
public class CadastrarPrescricaoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CadastrarPrescricaoServlet() {
        
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Prescricao p = new Prescricao();

            String dataPresc = request.getParameter("Data");
            String medicamento = request.getParameter("Prescrito");
            String CRM = request.getParameter("Responsavel");
            String dose = request.getParameter("Dose");
            String prescrito = request.getParameter("Prescrito");
            String quantidade = request.getParameter("Quantidade");
            String evolucao = request.getParameter("Evolucao");
            String registro = request.getParameter("Registro");
        //    String nomeMae = request.getParameter("nomeMae");

            DateFormat fmt = new SimpleDateFormat("dd/MM/yy");
        try {
            java.sql.Date data = new java.sql.Date(fmt.parse(dataPresc).getTime());
            p.setData(data);
            p.setCRM(Integer.parseInt(CRM));
            p.setMedicamento(medicamento);
            p.setDose(dose);
            p.setPrescrito(prescrito);
            p.setQuantidade(Float.parseFloat(quantidade));
            p.setEvolucao(evolucao);
            p.setRegistro(Integer.parseInt(registro));

         
        } catch (ParseException ex) {
            Logger.getLogger(CadastrarPrescricaoServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
         
        try {
            Conexao cnx = new Conexao();
            cnx.adiciona(p);

            request.setAttribute("medico", p.getCRM());
            RequestDispatcher dispatcher = request.getRequestDispatcher("medico/localizar_registro.jsp");
            dispatcher.forward(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(CadastrarPrescricaoServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
             
	}
       

}
